package io.virtualapp.home.adapters;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;
import java.util.List;

import io.virtualapp.R;
import io.virtualapp.mylibrary.util.Utils;

/********************
 *  Created by ：  Zeoy 
 *         Date：  2018\5\16 0016.
 * Introduction：   图片选择编辑适配器
 *********************/

public class ImageChoseAdapter extends RecyclerView.Adapter<ImageChoseAdapter.MyViewHolder> {
    private int image_num;
    private List<String> datas;
    private  Context context;
    private OnItemClickListener listener;
    public ImageChoseAdapter(Context context, int num, List<String> datas, OnItemClickListener listener){
        this.image_num = num;
        this.datas = datas;
        this.context = context;
        this.listener = listener;
        if (this.datas == null){
            this.datas = new ArrayList<>();
        }
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.su_item_image_choose_layout,null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (position>=datas.size()){
            holder.iv_del.setVisibility(View.GONE);
            holder.imageView.setImageResource(R.mipmap.su_tianjia);
            holder.imageView.setOnClickListener(v -> {
                listener.OnAddItemClick(image_num-datas.size());
            });
        }else {
            try {
              //  ImageLoader.getInstance().loadImage(context,holder.imageView, Uri.parse(datas.get(position)));
                Glide.with(context).load(Uri.parse(datas.get(position)))
                        .diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.imageView);
            }catch (Exception e){
                //ImageLoader.getInstance().loadImage(context,holder.imageView, datas.get(position));
                Glide.with(context).load(Uri.parse(datas.get(position)))
                        .diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.imageView);
            }

            holder.imageView.setOnClickListener(v -> {
                listener.OnItemClick(position);
            });

            holder.iv_del.setVisibility(View.VISIBLE);
            holder.iv_del.setOnClickListener(v -> {
                datas.remove(position);
                this.notifyDataSetChanged();
            });

        }
    }

    @Override
    public int getItemCount() {
        return datas.size()<image_num?datas.size()+1:datas.size();
    }

    class  MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private ImageView iv_del;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv);
            iv_del = itemView.findViewById(R.id.iv_del);
        }
    }

    public interface OnItemClickListener{
        void OnItemClick(int position);
        void OnAddItemClick(int count);
    }

    /**
     * 添加数据
     * @param dat
     */
    public  void AddDatas(List<String> dat){
        if (dat!=null){
            this.datas.addAll(dat);
           this.notifyDataSetChanged();
        }
    }

    /**
     * 添加单条数据
     * @param string
     */
    public void AddAloneData(String string){
        if (Utils.isNotEmpty(string)){
            this.datas.add(string);
            this.notifyItemInserted(this.datas.size()-1);
        }
    }

    /**
     * 替换单条数据
     * @param string
     * @param position
     */
    public void repeleceData(String string ,int position){
        if (Utils.isNotEmpty(string)&&position<this.datas.size()){
            this.datas.remove(position);
            this.datas.add(position,string);
            this.notifyItemChanged(position);
        }
    }

    public List<String> getDatas() {
        return datas;
    }
}
